//
//  HomeViewController.m
//  news_ios_app
//
//  Created by apple on 16/3/19.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HomeViewController.h"
#import <MJRefresh.h>
#import "AnimationView.h"
#import "MainViewModel.h"
#import "ArticleCell.h"

typedef NS_ENUM(NSInteger,ScrollStatus) {
    ScrollStatusHome,
    ScrollStatusList
};

@interface HomeViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property (weak,nonatomic) UITableView *tablevView;
@property (strong,nonatomic) UIView *navigationView;
@property (assign,nonatomic) ScrollStatus  scrollStatus;
@property (strong,nonatomic) AnimationView *view2222;
@property (assign,nonatomic) NSInteger pageNumber;
@end

@implementation HomeViewController

-(void)addHeadFresh
{
    WS(ws)
    self.tablevView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        ws.pageNumber = 1;
        [MainViewModel getNewWithTypeId:@"1" pageNumber:ws.pageNumber newsRusault:^(id data) {
            [ws setDataArr:data];
            [ws.tablevView reloadData];
            [ws.tablevView.mj_header endRefreshing];
        } ];
    }];
    [self.tablevView.mj_header beginRefreshing];
}

-(void)addFootFresh
{
    WS(ws)

    self.tablevView.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
        ws.pageNumber += 1;
        [MainViewModel getNewWithTypeId:@"0" pageNumber:ws.pageNumber newsRusault:^(id data) {
            [ws setDataArr:data];
            [ws.tablevView reloadData];
            [ws.tablevView.mj_footer endRefreshing];
        } ];
    }];
}


-(void)addShadow
{
    self.view.layer.shadowColor = [UIColor lightGrayColor].CGColor;//shadowColor阴影颜色
    self.view.layer.shadowOffset = CGSizeMake(-4,180 * kPROPORTION);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    self.view.layer.shadowOpacity = 0.8;//阴影透明度，默认0
    self.view.layer.shadowRadius = 6;//阴影半径，默认3
}

-(void)initNavigationView
{
    _navigationView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, BOUNDWIDTH, 0)];
    _navigationView.backgroundColor = [UIColor grayColor];
}

-(UIView *)createTableViewHeadView
{
    UIView *viewdddd = [[UIView alloc]initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, 200 * kPROPORTION)];
    
    viewdddd.backgroundColor = [UIColor clearColor];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 100 * kPROPORTION, BOUNDWIDTH, 40)];
    
    button.backgroundColor = [UIColor greenColor];
    [button addTarget:self action:@selector(aaaaaaaa) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"w我是按钮" forState:UIControlStateNormal];
    [viewdddd addSubview:button];
    
    _view2222 = [[AnimationView alloc]initWithFrame:CGRectMake(0, -200 * kPROPORTION, BOUNDWIDTH, 200 * kPROPORTION) contentView:viewdddd scrollView:_tablevView];
    [_view2222 updatePathWithContentOffset:CGPointMake(0, -200)];
    return _view2222;
}

-(void)aaaaaaaa
{
    NSLog(@"12765435654");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _pageNumber = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    _tablevView = self.baseTableView;
    _tablevView.backgroundColor = [UIColor whiteColor];
    _tablevView.frame = CGRectMake(0, 64, BOUNDWIDTH, BOUNDHEIGHT-64);
    [self.view addSubview:_tablevView];
    
    [_tablevView registerClass:[ArticleCell class] forCellReuseIdentifier:[ArticleModel cellType]];

//    [self initNavigationView];
//    
//    [self.view addSubview:_navigationView];
    
    _scrollStatus = ScrollStatusHome;
    [self addShadow];
    [self addHeadFresh];
    [self addFootFresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [super scrollViewDidScroll:scrollView];
    /*
    CGPoint contentOffset = scrollView.contentOffset;
    if (_scrollStatus == ScrollStatusHome) {
        CGRect frame = _view2222.frame;
        frame.origin.y = contentOffset.y;
        frame.size.height = -contentOffset.y;
        _view2222.frame = frame;
        
        if (scrollView.contentOffset.y >= -70) {
            
        }
        
        if (scrollView.contentOffset.y >= -64) {
            [_tablevView setContentInset:UIEdgeInsetsMake(64, 0, 0, 0)];
            
            if (contentOffset.y == -64) {
                [UIView animateWithDuration:0.3 animations:^{
                    _navigationView.frame = CGRectMake(0, 0, BOUNDWIDTH, 64);
                    _view2222.alpha = 0;

                } completion:^(BOOL finished) {
                    _scrollStatus = ScrollStatusList;
                    [self addHeadFresh];
                }];
            }
        }
    }
    [_view2222 updatePathWithContentOffset:scrollView.contentOffset];
  */
}

//-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
//{
//    targetContentOffset->y = -64;
//}


//-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
//{
//        if (_scrollStatus == ScrollStatusHome) {
//            if (scrollView.contentOffset.y <= -280) {
//                [_tablevView setContentOffset:CGPointMake(0, -64) animated:YES];
//            }
//        }
//}

@end
